package com.orm.mybatisplus.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import oracle.jdbc.xa.client.OracleXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
@MapperScan(basePackages = com.orm.mybatisplus.config.Master3DataSourceConfig.PACKAGE,sqlSessionTemplateRef = "master3SqlSessionTemplate")
public class Master3DataSourceConfig {
     static final String PACKAGE = "com.orm.mybatisplus.mapper.master3";
     static final String MAPPER_LOCATION = "classpath:mapper/master3/*.xml";


     @Value("${oracle.url}")
     private String oracleUrl;

     @Value("${oracle.username}")
     private String oracleUser;

     @Value("${oracle.password}")
     private String oraclePassword;

    @Value("${oracle.driverClassName}")
    private String driverClassName;

//     @Bean(name = "master3DataSource")   //非分布式事务数据源
//     public DataSource master3DataSource(){
//         HikariDataSource hikariDataSource = new HikariDataSource();
//         hikariDataSource.setJdbcUrl(oracleUrl);
//         hikariDataSource.setUsername(oracleUser);
//         hikariDataSource.setDriverClassName(driverClassName);
//         hikariDataSource.setPassword(oraclePassword);
//         hikariDataSource.setMinimumIdle(5);
//         hikariDataSource.setMaximumPoolSize(20);
//         hikariDataSource.setAutoCommit(true);
//         hikariDataSource.setPoolName("SpringBootDemoHikariCP");
//         hikariDataSource.setMaxLifetime(60000);
//         hikariDataSource.setConnectionTimeout(30000);
//         return  hikariDataSource;
//     }

     @Bean(name = "master3DataSource")   //分布式事务数据源
     public DataSource master2DataSource() throws SQLException {
         OracleXADataSource oracleXaDataSource = new OracleXADataSource();
         oracleXaDataSource.setURL(oracleUrl);
         oracleXaDataSource.setPassword(oraclePassword);
         oracleXaDataSource.setUser(oracleUser);
         oracleXaDataSource.setNativeXA(true);
         // 将本地事务注册到创 Atomikos全局事务
         AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
         xaDataSource.setXaDataSource(oracleXaDataSource);
         xaDataSource.setUniqueResourceName("master3DataSource");
         xaDataSource.setMinPoolSize(5);
         xaDataSource.setMaxPoolSize(20);
         xaDataSource.setMaxLifetime(60000);
         xaDataSource.setBorrowConnectionTimeout(30);
         xaDataSource.setLoginTimeout(30);
         xaDataSource.setMaintenanceInterval(60);
         xaDataSource.setMaxIdleTime(60);
         return xaDataSource;
     }

// 非分布式事务
//     @Bean(name = "master3TransactionManager")
//     public DataSourceTransactionManager masterTransactionManager(@Qualifier("master3DataSource") DataSource dataSource){
//        return new DataSourceTransactionManager(dataSource);
//     }

    @Bean(name = "master3SqlSessionFactory")
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("master3DataSource") DataSource dataSource) throws Exception {
        final MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.orm.mybatisplus.entity");
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        sqlSessionFactoryBean.setConfiguration(configuration);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(com.orm.mybatisplus.config.Master3DataSourceConfig.MAPPER_LOCATION));
        return sqlSessionFactoryBean.getObject();
    }

     @Bean(name = "master3SqlSessionTemplate")
     public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("master3SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
         return new SqlSessionTemplate(sqlSessionFactory);
     }

}
